RPN Calculator

Try out this reverse Polish notation calculator. It works like some popular scientific calculators. Instead of tapping 5 + 3, you tap "5", "Enter", "3", "+". Try these:


: STACK


Discussion

Unlike the previous calculator example, this RPN calculator stores arguments in a stack. Operators are entered in postfix order using reverse Polish notation. Each operation pops the stack by one. The results are shown in the answer line and must be manually pushed back onto the stack with the "Enter" key. A simple array called stack holds the stack data. The variable sktop points to the top of the stack. The function below demonstrates a stack push. After each push, the answer line is reset to zero and the stack line is updated.
function push(aform)
{
    // push onto stack
    stack[sktop] = ""+aform.answer.value
    
    // increase the top of the stack
    sktop++
    
    // reset the answer to zero
    aform.answer.value = "0"
    
    // show the new stack
    showstack(aform)
    
    // reset for entering a new number
    restart = true
    entry = ""
}
Copyright ©1998 by Charles River Media, All Rights Reserved